﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Common;


namespace WXPackage
{
    /// <summary>
    /// 公众账号配置信息
    /// </summary>
    public class ConfigInfo
    {
        /// <summary>
        /// 请求验证，自定义token
        /// </summary>
        public string Token { get; set; }
        /// <summary>
        /// 微信账号
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// 应用ID
        /// </summary>
        public string AppID { get; set; }
        /// <summary>
        /// 应用 AppSecret
        /// </summary>
        public string AppSecret { get; set; }
        /// <summary>
        /// 和微信服务器，交互，状态值
        /// </summary>
        public string Access_Token
        {
            get { return AccessTokenManage.AccessToken; }
        }

        private static ConfigInfo config = new ConfigInfo();
        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <returns></returns>
        public static ConfigInfo GetInfo()
        {
            if (config == null)
            {
                try
                {
                    //从磁盘加载对象
                    string filename = ConfigValue.DataPath + "wxconfig.xml";
                    XmlSerializerHelper _serialize = new XmlSerializerHelper(filename);
                    ConfigInfo config = _serialize.Deserialize<ConfigInfo>();
                }
                catch (Exception ex)
                {
                    LogOperate.Write("加载 ConfigInfo 失败，" + ex.Message);
                }
            }
            return config;
        }
        /// <summary>
        /// 修改配置信息
        /// </summary>
        /// <returns></returns>
        public static void UpdateInfo(ConfigInfo con)
        {
            config = con;
            //序列化到磁盘
            try
            {
                //保存到磁盘
                string filename = ConfigValue.DataPath + "wxconfig.xml";
                XmlSerializerHelper _serialize = new XmlSerializerHelper(filename);
                _serialize.Serialize<ConfigInfo>(con);
            }
            catch (Exception ex)
            {
                LogOperate.Write("序列化配置配置信息异常，" + ex.Message);
            }
        }
        public static void AddTest()
        {
            ConfigInfo config = new ConfigInfo();
            config.AppID = "wx145b4a8fd07b24e8";
            config.AppSecret = "fe6951dcb99772411c42f724b1336065";
            config.UserName = "gh_2ffac0e030f8";
            config.Token = "servicewx";
            UpdateInfo(config);
        }


    }
}
